<?php
class Admin extends Controller
{
	
	function __construct()
	{
		parent::Controller();
		$this->quickauth->restrict('admin');
		$this->load->model('admin_model','model');
		$this->load->helper('file');
	}
	
	function Admin()
	{
		self::__construct();
	}
	
	function index()
	{
		$modules['modules'] = $this->functions->get_modules();
		$this->load->view('header');
		$this->load->view('manage', $modules);
		$this->load->view('footer');
	}
	
	function add(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('file', 'File', 'required');
		//if ($this->form_validation->run() == FALSE)
		if (!($this->input->post('submit')))
		{
			$this->load->view('header');
			$this->load->view('add');
			$this->load->view('footer');
		}
		else
		{
			$name = $this->input->post('name');
			$url = $this->input->post('url');
			$description = $this->input->post('description');
			
			$config['upload_path'] = './modules/';
			$config['allowed_types'] = 'zip';
			$this->load->library('upload', $config);
			
			if ( ! $this->upload->do_upload('file'))
			{
				$error = array('error' => $this->upload->display_errors());
				$this->load->view('header');
				$this->load->view('add', $error);
				$this->load->view('footer');
			}	
			else
			{
				$data_arr = $this->upload->data();
				$data['ziplog'] = $this->functions->unzip('./modules/'.$data_arr['file_name'],'./modules');

				chmod('./modules/'.$data_arr['file_name'],777);
				unlink('./modules/'.$data_arr['file_name']);
				
				if( file_exists('modules/'.$data_arr['raw_name'].'/sql.php') ){
					$data['sqllog'] = $this->functions->sql_import('modules/'.$data_arr['raw_name'].'/sql.php');
				}
				
				
				if( file_exists('modules/'.$data_arr['raw_name'].'/info.php') ){
					require_once('modules/'.$data_arr['raw_name'].'/info.php');
					$i = array(
						'name' => $info['name'] ,
						'description' => $info['description'],
						'projecturl' => $info['projecturl']
					);
					$this->db->insert('module_list', $i);
				}
				
				$this->model->append_log(
					$_SESSION['username'],
					'Core',
					"Installed the " . $data_arr['raw_name'] . " plugin"
				);
				
				$data['success'] = "File " . $data_arr['file_name'] . " has been uploaded and installed.";
				$this->load->view('header');
				$this->load->view('log', $data);
				$this->load->view('footer');
			}
			
		}
	}
	
	function delete($id){
		
		$this->db->where('id', $id);
		$this->db->limit(1);
		$query = $this->db->get('module_list');
		$this->load->dbforge();
		foreach ($query->result_array() as $row){ $module_name = $row['name']; }
		
			require_once('modules/'.$module_name.'/sql.php');

			
			foreach($sql_array as $item){
				if( isset($item['create']) ){
					$this->dbforge->drop_table($item['create']);
				}
			}
		
		$this->db->where('id', $id);
		$this->db->limit(1);
		$query = $this->db->delete('module_list');
		
		$this->load->helper('file');
		$this->functions->chmodr('./modules/'.$module_name.'/',777);
		@delete_files('./modules/'.$module_name, TRUE);
		@delete_files('./modules/'.$module_name, FALSE);
		@unlink('./modules/'.$module_name);
		
		$this->model->append_log(
			$_SESSION['username'],
			'Core',
			"Uninstalled the " . $module_name . " plugin"
		);
		
		$data['message'] = 
		"<h2>Deleted</h2>
		<p>The " . $module_name . " Database information has been deleted, and the deletion of the installed files was attempted. Please use your FTP program to check that the module has been deleted completely.</p>
		<p>Check here:</p>
		<pre>" . getcwd() . "/modules/" . $module_name . "</pre>";
		$this->load->view('header');
		$this->load->view('landing',$data);
		$this->load->view('footer');
	}
}
